package com.mmw.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.ProductPageBo;
import com.mmw.crm.entity.Product;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.ProductService;
import com.mmw.crm.vo.CustomerVo;
import com.mmw.crm.vo.ProductSaveEntityVo;
import com.mmw.crm.vo.ProductSaveVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class ProductController {

    @Resource
    ProductService productService;

    @PostMapping("/CrmProduct/queryPageList")
    public Result crmProductQueryPageList(@RequestBody CustomerVo customerVo){
        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();

        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)){
            wrapper.like(Product::getName,search);
        }
        Page<Product> pageInfo = new Page<>(page,limit);
        productService.page(pageInfo,wrapper);

        List<Product> records = pageInfo.getRecords();
        ProductPageBo productPageBo = new ProductPageBo();
        productPageBo.setList(records);
        productPageBo.setTotalRow(records.size());
        return Result.build(productPageBo,null,0);
    }

    @PostMapping("/CrmProductCategory/queryList")
    public String crmProductCategoryQueryList(){
       String res = productService.crmProductCategoryQueryList();
       return res;
    }

    @PostMapping("/CrmProduct/saveAndUpdate")
    public Result saveAndUpdate(@RequestBody ProductSaveVo productSaveVo){
        ProductSaveEntityVo entity = productSaveVo.getEntity();
        Product product = new Product();
        product.setCategoryId(entity.getCategoty_id());
        product.setDescription(entity.getDescription());
        product.setName(entity.getName());
        product.setNum(entity.getNum());
        product.setPrice(entity.getPrice());
        product.setCreateTime(new Date());
        product.setCreateUserId(1L);
        product.setBatchId("1");
        productService.save(product);

return Result.build(null,null,0);
    }



}
